﻿
Partial Class UCMain_Signup
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        If IsNothing(Session("section")) Then
            Session.Clear()
            Response.Redirect("../Login.aspx")
        End If

        If Not IsPostBack Then
            img.Visible = False
            lblerror.Text = ""
            clearscreen()
            If Not IsNothing(Request.QueryString("ID_USER")) Then
                ' edit
                loadData()
            Else
                ' add

            End If

        End If
    End Sub

    Sub clearscreen()
        txtName.Text = ""
        TxtCompany.Text = ""
        TxtSurname.Text = ""
        TxtTel.Text = ""
        TxtPICOPno.Text = ""
    End Sub

    Sub loadData()
        Dim sql As String = ""
        ' sql = " select * from user_table where section='MMC'"
        sql = " select * from user_table where 1=1"
        ' sql = " select * from user_table "
        'If ddlSection.SelectedValue <> "0" Then
        '    sql += " and [ID_USER] = " & Request.QueryString("ID_USER") & "'"
        'End If
        If Not IsNothing(Request.QueryString("ID_USER")) Then
            sql += " and [ID_USER] = " & Request.QueryString("ID_USER")
            ddlSection.Visible = False
        End If
        ' sql += " order by ID_PICOP "
        Dim dt As New Data.DataTable
        dt = clsCommand.SQL_exec(sql)
        If dt.Rows.Count > 0 Then
            txtName.Text = dt.Rows(0)("firstname").ToString
            TxtCompany.Text = dt.Rows(0)("company").ToString
            TxtSurname.Text = dt.Rows(0)("lastname").ToString
            TxtTel.Text = dt.Rows(0)("tel").ToString
            'Dim picop As String() = dt.Rows(0)("id_picop").ToString.Split("PICOP")
            TxtPICOPno.Text = dt.Rows(0)("id_picop")
            ddlSection.SelectedValue = dt.Rows(0)("Section").ToString
        End If
    End Sub

    Function checkBeforeSave() As Boolean
        Dim rs As Boolean = True
        Dim msg As String = " กรุณาระบุ "
        img.Visible = True
        lblerror.Visible = True

        If IsNothing(Request.QueryString("ID_USER")) Then
            If ddlSection.SelectedValue = "0" Then
                msg += " Section "
                rs = False
            End If
        End If


        If txtName.Text = "" Then
            msg += " , Name"
            rs = False
        End If

        If TxtSurname.Text = "" Then
            msg += " , Surname"
            rs = False
        End If

        If TxtTel.Text = "" Then
            msg += " , Tel"
            rs = False
        End If

        If TxtCompany.Text = "" Then
            msg += " , Company "
            rs = False
        End If

        If rs = False Then
            img.Visible = True
            lblerror.Visible = True
            lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "W001", msg)
        End If

        Return rs
    End Function


    Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If checkBeforeSave() Then
            If Not IsNothing(Request.QueryString("ID_USER")) Then
                ' edit
                Dim sqle As String = ""
                Dim id_user As String = Request.QueryString("ID_USER")
                sqle = " update [user_table] set "
                sqle += " ID_PICOP='" & TxtPICOPno.Text & "'"
                sqle += " ,PASSWORD_PICOP='" & Left(TxtPICOPno.Text, 3) & "'"
                sqle += " ,FIRSTNAME='" & txtName.Text & "'"
                sqle += " ,LASTNAME='" & TxtSurname.Text & "'"
                'sqle += " ,[SECTION]='" & ddlSection.SelectedValue & "'"
                sqle += " ,TEL='" & TxtTel.Text & "'"
                sqle += " ,Company= '" & TxtCompany.Text & "'"
                sqle += " where ID_USER= " & id_user & ""

                Dim err As String = ""
                err = clsCommand.SQL_cmd(sqle)
                If err = "" Then
                    'img.Visible = True
                    'lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "I001")
                    Utility.clsAlertMsg.alert_location(Page, "บันทึกข้อมูลเรียบร้อย", "EditSectionProfile.aspx")
                Else
                    img.Visible = True
                    lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", err)
                End If
            Else
                ' add
                Dim sqlin As String = ""
                sqlin = " insert into [user_table] (ID_PICOP,PASSWORD_PICOP,FIRSTNAME,LASTNAME,[SECTION],TEL,Company) "
                sqlin += " values ('" & TxtPICOPno.Text & "'"
                sqlin += " ,'P" & Left(TxtPICOPno.Text, 3) & "'"
                sqlin += " ,'" & txtName.Text & "'"
                sqlin += " ,'" & TxtSurname.Text & "'"
                sqlin += " ,'" & ddlSection.SelectedValue & "'"
                sqlin += " ,'" & TxtTel.Text & "'"
                sqlin += ", '" & TxtCompany.Text & "')"

                Dim err As String = ""
                err = clsCommand.SQL_cmd(sqlin)
                If err = "" Then
                    'img.Visible = True
                    'lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "I001")
                    Utility.clsAlertMsg.alert_location(Page, "บันทึกข้อมูลเรียบร้อย", "EditSectionProfile.aspx")
                Else
                    img.Visible = True
                    lblerror.Text = Utility.clsAlertMsg.infoMessage(img, "E004", err)
                End If
            End If

        End If
    End Sub

   
End Class
